<?php

class ReportSaleClass 
{
	public $_explicitType = "presstrack.ReportSaleClass";
    
	public $arMonthBySale;
	public $arManager;
		
	static function GetSaleReport($year)
	{
		set_time_limit(0);	
		$result->status = false;
		$report = new ReportSaleClass();
		
		// array of manager
		$sql ="SELECT * FROM press_track_new.user WHERE medium = -1 "
			 . "AND section = 1";
		$query = mysql_query($sql);
		if (mysql_errno())
		{
			$result->message = mysql_errno() . ": " . mysql_error();
			return $result;
		}		
		$ar_of_manager = array();
		while ($res = mysql_fetch_object($query)) 
		{
			$ar_of_manager[] = $res;
		}
		
		// sum of orders
		for ($i=1; $i < 13; $i++) 
		{
			$month = $i;
			if ($i < 10)
			{
				$month = "0" . $month;
			}
			$date = $month . "/" . $year;
			
			$ar_by_month = array();
			foreach ($ar_of_manager as $manager)
			{
				$sql = "SELECT SUM(cost) AS sum_of_order FROM task "
					 . "WHERE task.order "
					 . "IN (SELECT idorder FROM press_track_new.order "
					 . "WHERE press_track_new.order.manager = $manager->iduser "
					 . "AND press_track_new.order.creation LIKE '%$date')";
				$query = mysql_query($sql);
				$res = mysql_fetch_object($query);
				$row->name = $manager->name;
				$row->first = 0;
				$row->second = 0;
				
				if ($res->sum_of_order != null)
				{
					$row->first = round($res->sum_of_order, 2);
					$ar_by_month[] = clone $row;
				}				
			}
			$report->arManager[] = $ar_by_month;
			$ar_by_month		 = null;
		}
		
		$i = 1;
		foreach ($report->arManager as $month)
		{
			$m = $i;
			if ($i < 10)
			{
				$m = "0" . $m;
			}
			$row2->name		= $m;
			$row2->first 	= 0;
			foreach ($month as $sum)
			{
				$row2->first = $row2->first + $sum->first;				
			}
			$report->arMonthBySale[] = clone $row2;
			$i++;
		}
		
		/*	$sql = "SELECT SUM(cost) AS sum_of_order FROM task "
				 . "WHERE task.order "
				 . "IN (SELECT idorder FROM press_track_new.order "
				 . "WHERE press_track_new.order.creation LIKE '%$date')";
			$query = mysql_query($sql);
			if (mysql_errno())
			{
				$result->message = mysql_errno() . ": " . mysql_error();
				return $result;
			}
			$res = mysql_fetch_object($query);
			$row->name	= $month;
			$row->first	= 0;
			if ($res->sum_of_order != null)
			{				
				$row->first	= round($res->sum_of_order, 2);
			}
			$report->arMonthBySale[] = clone $row;*/
		
		$result->status = true;
		$result->report = $report;
		return $result;
	}	
}
?>